/*
 * 文件名：LEDControl.cpp
 * 功能描述：WS2812 LED灯带控制
 */
#include "LEDControl.h"
//RGB led库
Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);  // 在文件内部初始化strip对象

unsigned long lastTime = 0;
bool ledState = false;
int blinkCount = 0;
int currentBlinkCount = 0;
float brightness = 0.3; 

// 初始化LED控制系统
void initLEDControl() {
    strip.updateLength(NUM_LEDS); // 更新LED数量
    strip.setPin(PIN);  // 设置引脚
    strip.begin();      // 初始化LED
    strip.clear();      // 清除LED颜色
    strip.show();       // 更新LED显示
}

// 设置LED常亮颜色
void setConstantColor(int ledIndex, uint8_t red, uint8_t green, uint8_t blue) {
    strip.setPixelColor(ledIndex, strip.Color(red * brightness, green * brightness, blue * brightness));
    strip.show();
}

// 设置LED闪烁颜色
void setBlinkingColor(int ledIndex, uint8_t red, uint8_t green, uint8_t blue, int blinkInterval, int blinkCount) {
    unsigned long currentTime = millis();

    if (currentTime - lastTime >= blinkInterval) {
        ledState = !ledState;
        if (ledState) {
            strip.setPixelColor(ledIndex, strip.Color(red * brightness, green * brightness, blue * brightness));
        } else {
            strip.setPixelColor(ledIndex, strip.Color(0, 0, 0));
        }
        lastTime = currentTime;

        // 如果闪烁次数达到，停止闪烁
        if (++currentBlinkCount >= blinkCount+1) {
            strip.setPixelColor(ledIndex, strip.Color(0, 0, 0));  // 关闭LED
            strip.show();
            currentBlinkCount = 0;
        }
    }
    strip.show();  // 更新LED状态
}

// 关闭LED
void turnOffLED(int ledIndex) {
    strip.setPixelColor(ledIndex, strip.Color(0, 0, 0));
    strip.show();
}
